//
//  LoginViewController.m
//  Project_IOS
//
//  Created by Jeff on 2022/1/11.
//

#import "LoginViewController.h"
#import "LoginNumberView.h"
#import "ResetPWViewController.h"
#import "LoginAgreementView.h"
#import "LoginTools.h"
#import <Masonry/Masonry.h>
#import "Color.h"
@interface LoginViewController ()
@property (nonatomic, strong) UIView *centerView;

@property (nonatomic, strong) LoginNumberView *phoneView;
@property (nonatomic, strong) LoginNumberView *pwView;
@property (nonatomic, strong) LoginAgreementView *agreementV;
@property (nonatomic, strong) UILabel *codeLb;
@property (nonatomic, strong) UILabel *forgetLb;
@property (nonatomic, strong) UIButton *fastBtn;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.titleStr = @"密码登录";
    
//    [self.view addSubview:self.centerView];
//    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.mas_equalTo(CGSizeMake(343), 500)));
//            make.top.mas_equalTo(137));
//            make.centerX.mas_equalTo(0);
//    }];
    
    self.phoneView = [[LoginNumberView alloc] initPhone:true];
    [self.phoneView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    if (LoginTools.currentLoginUser != nil) {
        self.phoneView.textField.text = LoginTools.currentLoginUser.mobile;
    }
    
    self.pwView = [[LoginNumberView alloc] initPhone:false];
    [self.pwView.textField addTarget:self action:@selector(textfieldContentChange:) forControlEvents:UIControlEventEditingChanged];
    self.buttonView.btnStr = @"密码登录";
    
    self.agreementV = [LoginAgreementView new];
    
    
    [self.buttonView.button addTarget:self action:@selector(loginBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    self.buttonView.avable = false;
    
    [self.contentView addSubview:self.phoneView];
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(110);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    [self.contentView addSubview:self.pwView];
    [self.pwView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(311, 52));
    }];
    
    [self.contentView addSubview:self.agreementV];
    [self.agreementV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(311, 30));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.pwView.mas_bottom).offset(5);
    }];
    
    [self.contentView addSubview:self.buttonView];
    [self.buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(311, 48));
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(self.agreementV.mas_bottom).offset(20);
    }];
    
    [self.contentView addSubview:self.codeLb];
    [self.codeLb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.buttonView.mas_right).offset(-5);
            make.top.mas_equalTo(self.pwView.mas_bottom).offset(12);
    }];
    
    self.fastBtn.hidden = LoginTools.currentLoginUser.is_guest != YES;
    [self.contentView addSubview:self.fastBtn];
    [self.fastBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(self.buttonView);
        make.top.equalTo(self.buttonView.mas_bottom).offset(8);
    }];
}

- (void)loginBtnOnClick {
//    [MobClick event:@"SigninClick"];
//    if (!self.agreementV.btn.isSelected) {
//        [MBProgressHUD showMessage:@"请勾选同意《用户协议》和《隐私政策》！"];
//        return;
//    }
//    [self.pwView.textField resignFirstResponder];
//    [self.phoneView.textField resignFirstResponder];
//    if (self.phoneView.textField.text.length != 11 || self.pwView.textField.text.length == 0) {
//        [MBProgressHUD showMessage:@"请检查输入项！"];
//        return;
//    }
    
    [LoginTools login:self.phoneView.textField.text pwd:self.pwView.textField.text success:^{
        if (self.loginCallback) {
            self.loginCallback(YES);
        }
        [self dismiss];
    }];

}

- (void)labelClick {
    [self.pwView.textField resignFirstResponder];
    [self.phoneView.textField resignFirstResponder];
    
    if (self.phoneView.textField.text.length != 11) {
//        [MBProgressHUD showMessage:@"请输入正确的手机号码"];
        return;
    }
    ResetPWViewController *vc = [ResetPWViewController new];
    vc.mobile = self.phoneView.textField.text;
    [self.navigationController pushViewController:vc animated:YES];
}



- (UIView *)centerView {
    if (!_centerView) {
        _centerView = [[UIView alloc] init];
        _centerView.backgroundColor = [Color colorWithHexString:@"#FFFFFF"];
        _centerView.layer.cornerRadius = 15;
        _centerView.layer.masksToBounds = true;
    }
    return _centerView;
}



- (UILabel *)codeLb {
    if (!_codeLb) {
        _codeLb = [[UILabel alloc] init];
        _codeLb.font = [UIFont systemFontOfSize:12];
        _codeLb.textColor = [Color colorWithHexString:@"#999999"];
        _codeLb.text = @"忘记密码?";
        UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelClick)];
        
        [_codeLb addGestureRecognizer:gestureRecognizer];
        _codeLb.userInteractionEnabled = YES;
        
    }
    return _codeLb;
}

- (void)textfieldContentChange:(UITextField *)field {
    if (self.phoneView.textField.text.length == 11 && self.pwView.textField.text != nil && self.pwView.textField.text.length > 0) {
        self.buttonView.avable = true;
    }else{
        self.buttonView.avable = false;
    }
}

- (UIButton *)fastBtn {
    if (!_fastBtn) {
        _fastBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_fastBtn setTitle:@"已有游客会员账号，点击一键登录" forState:UIControlStateNormal];
        [_fastBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _fastBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_fastBtn addTarget:self action:@selector(actionFastLogin) forControlEvents:UIControlEventTouchUpInside];
    }
    return _fastBtn;
}

- (void)actionFastLogin {
    [LoginTools fastLogin:^{
        if (self.loginCallback) {
            self.loginCallback(YES);
        }
        [self dismiss];
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
